package com.asdc.autoscanfiles.repository;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.asdc.autoscanfiles.entity.ImgFileDto;

@Repository("imgFileRepository")
public class ImgFileRepository {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	// 批量保存数据
	public int[] batchAddImgFileProject(List<ImgFileDto> imgFiles) {
		
		//先删再加
		String deleteSql = "delete from qxj.img_file where file_name=?";
		List<Object[]> deleteParamList = new ArrayList<>();
		
		String sql = "INSERT INTO qxj.img_file "
				+ "(level1, level2, level3, path, "
				+ "new_path, element_type, "
				+ "area_number, begin_date,"
				+ "end_date, type, file_name,type_value,sheng, "
				+ "province_code, province_name, area_name"
				+ ") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		List<Object[]> paramsList = new ArrayList<>();
		for (ImgFileDto imgFile : imgFiles) {
			Object[] params = { imgFile.getLevel1(), imgFile.getLevel2(), imgFile.getLevel3(),
					imgFile.getPath(), imgFile.getNewPath(), imgFile.getElementType(),
					imgFile.getAreaNumber(), imgFile.getBeginDate(), imgFile.getEndDate(),
					imgFile.getType(), imgFile.getFileName(), imgFile.getTypeValue(),
					imgFile.getSheng(), imgFile.getProvinceCode(), imgFile.getProvinceName(),
					imgFile.getAreaName() };
			paramsList.add(params);
			Object[] deleteParams = {imgFile.getFileName()};
			deleteParamList.add(deleteParams);
		}
		this.jdbcTemplate.batchUpdate(deleteSql, deleteParamList);
		return jdbcTemplate.batchUpdate(sql, paramsList);
	}
}
